package com.sjgl.esdn.service.impl;

import com.sjgl.esdn.constant.UserConstantsESDN;
import com.sjgl.esdn.domain.entity.File;
import com.sjgl.esdn.domain.entity.Question;
import com.sjgl.esdn.domain.entity.User;
import com.sjgl.esdn.mapper.FileMapper;
import com.sjgl.esdn.mapper.UserMapper;
import com.sjgl.esdn.service.DeptService;
import com.sjgl.esdn.service.FileService;
import com.sjgl.esdn.service.UserService;
import com.sjgl.esdn.utils.ImageUtilsESDN;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户 服务层处理
 *
 * @author Administrator
 * @date 2022/08/02
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    @Autowired
    FileMapper fileMapper;

    @Autowired
    FileService fileService;

    @Autowired
    DeptService deptService;

    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public User selectUserById(Long userId) {
        User user = userMapper.selectUserById(userId);
        user.setDeptName(deptService.getDeptNameById(user.getDeptId()));
        return user;
    }

    @Override
    public User selectUserByPhonenumber(String phonenumber) {
        User user = userMapper.selectUserByPhonenumber(phonenumber);
        if(user==null){
            return null;
        }
        user.setDeptName(deptService.getDeptNameById(user.getDeptId()));
        return user;
    }

    /**
     * 校验用户ID是否唯一
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public String checkUserUnique(Long userId) {
        int count = userMapper.checkIdUnique(userId);
        if (count > 0) {
            return UserConstantsESDN.NOT_UNIQUE;
        }
        return UserConstantsESDN.UNIQUE;
    }

    /**
     * 检查手机唯一
     *
     * @param phonenumber phonenumber
     * @return {@link String}
     */
    @Override
    public String checkPhoneUnique(String phonenumber) {
        int count = userMapper.checkPhoneUnique(phonenumber);
        if (count > 0) {
            return UserConstantsESDN.NOT_UNIQUE;
        }
        return UserConstantsESDN.UNIQUE;
    }

    /**
     * 检查邮箱唯一
     *
     * @param email 电子邮件
     * @return {@link String}
     */
    @Override
    public String checkEmailUnique(String email) {
        int count = userMapper.checkEmailUnique(email);
        if (count > 0) {
            return UserConstantsESDN.NOT_UNIQUE;
        }
        return UserConstantsESDN.UNIQUE;
    }

    /**
     * 更新用户
     *
     * @param user 用户
     * @return int
     */
    @Override
    public int updateUser(User user) {
        if (user.getDeptName() != null && !Objects.equals(user.getDeptName(), "")) {
            user.setDeptId(deptService.getIdByDeptName(user.getDeptName()));
        }
        return userMapper.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     * @return int
     */
    @Override
    public int deleteUserById(Long userId) {
        return userMapper.deleteUserById(userId);
    }

    /**
     * 批量删除用户信息
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] userIds) {
        //权限问题
        return userMapper.deleteUserByIds(userIds);
    }

    /**
     * 插入用户
     *
     * @param user 用户
     * @return int
     */
    @Override
    public int insertUser(User user) {
        //不传入创建者，则默认为用户自己创建
        if (user.getCreateId() == null) {
            user.setCreateId(user.getUserId());
        }
        //部门名存在时，设置用户部门id
        if (user.getDeptName() != null && !Objects.equals(user.getDeptName(), "")) {
            user.setDeptId(deptService.getIdByDeptName(user.getDeptName()));
        }
        // 新增用户信息
        int rows = userMapper.insertUser(user);
        return rows;
    }

    @Override
    public String selectQuestionByQuestionId(Long questionId) {
        return userMapper.selectQuestionByQuestionId(questionId);
    }

    @Override
    public List<Question> selectQuestionList() {
        return userMapper.selectQuestionList();
    }

    @Override
    public void resetPasswordRequest(Long userId) {
        //若用户已申请过，先删除记录
        userMapper.deleteResetRequest(userId);
        userMapper.insertResetRequest(userId);
    }

    @Override
    public List<File> getUnpublishedBlogs(Long userId) {
        return fileMapper.getUnpublishedBlogs(userId);
    }

    /**
     * 获得用户头像
     *
     * @param userId 用户id
     * @return {@link String}
     */
    public String getUserAvatar(Long userId) {
        String path1 = fileService.selectFolder("avatar");
        String path = path1 + userId + ".jpg";
        if (!new java.io.File(path).exists()) {
            path = path1 + "0.jpg";
        }
        return ImageUtilsESDN.fileToBase64(path);
    }
}
